import FontAwesome from '@expo/vector-icons/FontAwesome';
import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { useFonts } from 'expo-font';
import { Stack } from 'expo-router';
import * as SplashScreen from 'expo-splash-screen';
import { useEffect } from 'react';
import 'react-native-reanimated';
import Modal from '@/app/toast/ToastModal';
import Toast from '@/app/toast/Toast';
import Loading from '@/app/toast/ToastLoading';
import WeiKaiFang from '@/app/toast/WeiKaiFang';
import PickerModal from '@/app/toast/PickerModal';

import Dialog from '@/app/toast/Dialog';
// 导入事件系统以初始化全局函数
import '@/app/toast/event';
import { useColorScheme } from '@/components/useColorScheme';

export {
  // Catch any errors thrown by the Layout component.
  ErrorBoundary,
} from 'expo-router';

// Removed unstable_settings as it's not working in Expo Router v6

// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();

export default function RootLayout() {
  const [loaded, error] = useFonts({
    SpaceMono: require('../assets/fonts/SpaceMono-Regular.ttf'),
    ...FontAwesome.font,
  });

  // Expo Router uses Error Boundaries to catch errors in the navigation tree.
  useEffect(() => {
    if (error) throw error;
  }, [error]);

  useEffect(() => {
    if (loaded) {
      SplashScreen.hideAsync();
    }
  }, [loaded]);

  if (!loaded) {
    return null;
  }

  return <RootLayoutNav />;
}

function RootLayoutNav() {
  const colorScheme = useColorScheme();
  return (
    <SafeAreaProvider>
      <ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
        <Toast />
        <Modal />
        <Loading />
        <Dialog />
        <WeiKaiFang />
        <PickerModal />
        <Stack  screenOptions={{ headerShown: false }}>
          <Stack.Screen name="pages/auth/LoginScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/auth/ForgetPasswordScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/auth/ForgetPasswordStep2Screen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/auth/ForgetPasswordStep3Screen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/auth/RegisteredScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/auth/ResetPasswordScreen" options={{ headerShown: false }} />
          <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
          <Stack.Screen name="pages/loading/LoadingScreen" options={{ headerShown: false }} />
          <Stack.Screen name="modal" options={{ presentation: 'modal' }} />
          <Stack.Screen name="pages/setting/settings" options={{ headerShown: false }} />
          <Stack.Screen name="pages/profile/profile" options={{ headerShown: false }} />
          <Stack.Screen name="pages/notification/notification" options={{ headerShown: false }} />
          <Stack.Screen name="pages/privacy/privacy" options={{ headerShown: false }} />
          <Stack.Screen name="pages/help/help" options={{ headerShown: false }} />
          <Stack.Screen name="pages/qrScanner/qRScanner" options={{ headerShown: false }} />
          <Stack.Screen name="pages/langeuageSettingPage/langSetting" options={{ headerShown: false }} />
          <Stack.Screen name="pages/webview/webview" options={{ headerShown: false }} />
          <Stack.Screen name="pages/UserInfoScreen/UserInfoScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/shareApp/ShareAppScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/shareApp/MyTeamLogsScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/shareApp/MyTeamScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/Notice/NoticeScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/Notice/NoticeDeatilScreen" options={{ headerShown: false }} />
          <Stack.Screen name="pages/UserRulesScreen/index" options={{ headerShown: false }} />
          <Stack.Screen name="pages/UserYSRulesScreen/index" options={{ headerShown: false }} />
          <Stack.Screen name="pages/AboutmreScreen/AboutmreScreen" options={{ headerShown: false }} />
        </Stack>
      </ThemeProvider>
    </SafeAreaProvider>
  );
}
